#include "mainwindow.h"

#include "manager.h"
#include "common_utilities.h"
#include "initialdialog.h"

#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <QSignalMapper>
#include <QLabel>
#include <QTimer>
#include <QDebug>
#include <QDir>
#include <QApplication>

namespace
{

static const QString buttonStyle = "QPushButton {"
                                   "    background-color: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 %1, stop:1 %2);"
                                   "    color: %3;"
                                   "    border: 1px solid %4;"
                                   "    border-radius: 10px;"
                                   "    font-size: 30px;"
                                   "    font-weight: bold;"
                                   "}";

static const QString neutralButtonStyle = buttonStyle.arg("rgb(192, 192, 192)", "white", "black", "gray");
static const QString negativeButtonStyle = buttonStyle.arg("rgb(100, 32, 32)", "rgb(192, 32, 32)", "white", "rgb(100, 0, 0)");
static const QString positiveButtonStyle = buttonStyle.arg("rgb(32, 100, 32)", "rgb(32, 192, 32)", "white", "rgb(0, 100, 0)");

QPushButton* CreateOptionButton(const QString& text)
{
    QPushButton* button = new QPushButton(text);
    button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    button->setFlat(true);
    button->setMinimumWidth(200);
    button->setMinimumHeight(150);
    button->setFocusPolicy(Qt::NoFocus);
    button->setStyleSheet(neutralButtonStyle);

    return button;
}

} // unnamed namespace

MainWindow::MainWindow(QWidget *parent)
    : QDialog(parent)
{
    m_signalMapper = new QSignalMapper(this);

    connect(m_signalMapper, SIGNAL(mapped(const QString &)),
                 this, SLOT(clicked(const QString &)));

    QHBoxLayout* hLayout = new QHBoxLayout;

    initialDialog d;
    if (QDialog::Rejected == d.exec()) {
        exit(0);
    }

    m_manager = Manager::getManager();
    m_manager->m_category = d.getCategories();
    m_manager->m_language = d.getLanguage();
    m_manager->loadTests();
    //m_manager->print();

    for (int i = 0; i < 6; ++i)
    {
        QPushButton* button = CreateOptionButton("");
        connect(button, SIGNAL(clicked()), m_signalMapper, SLOT(map()));
        m_signalMapper->setMapping(button, QString::number(i));
        m_options.push_back(button);
    }

    m_testImage = new QLabel;

    QVBoxLayout* vLayout1 = new QVBoxLayout;
    vLayout1->addWidget(m_options[0]);
    vLayout1->addWidget(m_options[3]);

    QVBoxLayout* vLayout2 = new QVBoxLayout;
    vLayout2->addWidget(m_options[1]);
    vLayout2->addWidget(m_testImage, 0, Qt::AlignHCenter);
    vLayout2->addWidget(m_options[4]);

    QVBoxLayout* vLayout3 = new QVBoxLayout;
    vLayout3->addWidget(m_options[2]);
    vLayout3->addWidget(m_options[5]);

    hLayout->addLayout(vLayout1);
    hLayout->addLayout(vLayout2);
    hLayout->addLayout(vLayout3);

    setLayout(hLayout);

    loadTest();
}

void MainWindow::clicked(const QString& id)
{
    if (id.toInt() == m_correctId) {
        m_options[m_correctId]->setStyleSheet(positiveButtonStyle);
        QTimer::singleShot(2000, this, SLOT(loadTest()));
    } else {
        m_options[id.toInt()]->setStyleSheet(negativeButtonStyle);
        m_options[id.toInt()]->setEnabled(false);
    }
    Manager::getManager()->updateTestStatistics(m_currentTestId, id.toInt() == m_correctId);
}

void MainWindow::loadTest()
{
    const Test test = m_manager->getNextTest();
    m_correctId = test.currentOptionId;
    m_currentTestId = test.id;

    for (int i = 0; i < 6; ++i)
    {
        m_options[i]->setText(test.optionsList[i].text);
        m_options[i]->setStyleSheet(neutralButtonStyle);
        m_options[i]->setEnabled(true);
    }
    QPixmap p(test.optionsList[test.currentOptionId].path);
    p = p.scaled(QSize(237, 213));
    m_testImage->setPixmap(p);
}

